//index.js
let timer;
let numAi = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageAiSrc:'',
    imageUserSrc:'/pages/images/4.jpeg',
    srcs:[
      '/pages/images/1.jpeg',
      '/pages/images/2.jpeg',
      '/pages/images/3.jpeg',
    ],
    winNum:0,
    gameOfPlay:'',
    btnState:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //let oldWinNum = wx.getStorageSync('winNum');
      if (wx.getStorageSync('winNum') != '' ){
        this.setData({ 'winNum': wx.getStorageSync('winNum')})
      }
    this.timerGo();
  },

  timerGo(){
    timer = setInterval(this.move,100);
  },
  move(){
      if (numAi >= 3) {
        numAi = 0;
      }
      this.setData({ imageAiSrc: this.data.srcs[numAi] });
      numAi++;

      //随机出拳
      //numAi = parseInt(Math.floor(Math.random()*3));
      //this.setData({ imageAiSrc: this.data.srcs[numAi] });
  },
  changeForChoose(e){
    if (this.data.btnState) {
      return;
    }
    this.setData({ imageUserSrc: this.data.srcs[e.currentTarget.id]});
    clearInterval(timer);

    let user = this.data.imageUserSrc;
    let ai = this.data.imageAiSrc;
    let num = this.data.winNum;
    let str = '你输了！';
    
    if (user == '/pages/images/1.jpeg' && ai == '/pages/images/2.jpeg' || user == '/pages/images/2.jpeg' && ai == '/pages/images/3.jpeg' || user == '/pages/images/3.jpeg' && ai == '/pages/images/1.jpeg'){
      num++;
      str = '你赢了！';
      wx.setStorageSync('winNum', num);
    }

    if (user == ai){
      str = '打平了！';
    }

    this.setData({ 'winNum': num, 'gameOfPlay': str,'btnState':true})
  },
  again(){
    if (!this.data.btnState){
      return;
    }
    this.timerGo();
    this.setData({ 'btnState': false, gameOfPlay: '', imageUserSrc: '/pages/images/4.jpeg'});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})